package com.example.ssmxuankeback.controller;

import com.example.ssmxuankeback.common.RestBean;
import com.example.ssmxuankeback.dto.UserDTO;
import com.example.ssmxuankeback.dto.UserPageQueryDTO;
import com.example.ssmxuankeback.service.UserService;
import com.example.ssmxuankeback.vo.PageVO;
import com.example.ssmxuankeback.vo.UserVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequiredArgsConstructor
@Validated
@Tag(name = "UserController", description = "用户管理接口")
@RequestMapping("/users")
public class UserController {

    private final UserService userService;

    @Operation(summary = "用户注册")
    @PostMapping("/register")
    public RestBean<UserVO> register(@RequestBody @Validated UserDTO dto) {
        return userService.register(dto);
    }

    @Operation(summary = "用户登录")
    @PostMapping("/login")
    public RestBean<UserVO> login(@RequestBody @Validated UserDTO dto) {
        return userService.login(dto);
    }

    @Operation(summary = "更新用户信息")
    @PutMapping("/{userId}")
    public RestBean<UserVO> updateUser(
            @Parameter(description = "用户ID", required = true)
            @PathVariable Long userId,
            @RequestBody @Validated UserDTO dto) {
        dto.setId(userId);
        return userService.update(dto);
    }

    @Operation(summary = "删除用户")
    @DeleteMapping("/{userId}")
    public RestBean<UserVO> deleteUser(
            @Parameter(description = "用户ID", required = true)
            @PathVariable Long userId) {
        return userService.delete(userId);
    }

    @Operation(summary = "根据ID查询用户详情")
    @GetMapping("/{userId}")
    public RestBean<UserVO> getUserById(
            @Parameter(description = "用户ID", required = true)
            @PathVariable Long userId) {
        return userService.getUserById(userId);
    }

    @Operation(summary = "分页查询用户列表")
    @GetMapping("/page")
    public RestBean<PageVO<UserVO>> getUserPage(@Validated UserPageQueryDTO queryDTO) {
        return userService.listByPage(queryDTO);
    }

    @Operation(summary = "根据班级ID查询用户列表")
    @GetMapping("/by-class/{classId}")
    public RestBean<List<UserVO>> getUsersByClassId(
            @Parameter(description = "班级ID", required = true)
            @PathVariable Long classId) {
        return userService.getUsersByClassId(classId);
    }
}